﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Shoozla.DataModel;
using System.Net;
using System.IO;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;
using Shoozla.View;


namespace Shoozla.ViewModel
{
    public class ID3TagEditorViewModel : ViewModelBase
    {

        private Mp3File _song;
        public Mp3File Song
        {
            get { return _song; }
            set
            {
                if (_song != value)
                {
                    _song = value;
                    OnPropertyChanged("Song");
                }
            }
        }

        private RelayCommand _CmdSaveSong;
        public RelayCommand CmdSaveSong
        {
            get
            {
                if (_CmdSaveSong == null)
                {
                    _CmdSaveSong = new RelayCommand(param => DoCmdSaveSong(), (o) =>
                    {
                        if (App.CurrentAppStatus == App.AppStatus.Idle && Song != null) return true;
                        else return false;
                    });
                }
                return _CmdSaveSong;
            }
        }

        private RelayCommand _CmdSearchSingleCover;
        public RelayCommand CmdSearchSingleCover
        {
            get
            {
                if (_CmdSearchSingleCover == null)
                {
                    _CmdSearchSingleCover = new RelayCommand(param => DoCmdSearchSingleCover(), (o) =>
                    {
                        if (App.CurrentAppStatus == App.AppStatus.Idle && Song != null) return true;
                        else return false;
                    });
                }
                return _CmdSearchSingleCover;
            }
        }


        public void DoCmdSaveSong()
        {
            try
            {
                IMP3Processor mp3Processor = new IDSharpProcessor();
                bool res = mp3Processor.SaveID3Tag(Song);
                if (res)
                {
                    Song.Dirty = false;
                    App.LogEntry("[SUCCESS] " + Song);
                    RaiseFileSaved(Song.FileName.FullName);
                }
                else
                    MessageBox.Show("File not saved", "Shoozla", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception e)
            {
                App.LogEntry("[ERROR] ID3TagEditorViewModel: " + e.Message);
                MessageBox.Show("File not saved: " + e.Message, "Shoozla", MessageBoxButton.OK, MessageBoxImage.Error);

            }
        }

        public void DoCmdSearchSingleCover()
        {
            //it asks which "website" to use
            SearchAddinView saw = new SearchAddinView(App.MainView, SelectedItems.Count);
            var confirm = saw.ShowDialog();
            if (confirm != true)
                return;

           Uri result = App.CoverSearcher.FindSingleCover(App.CurrentEditingSong);
           if (result != null)
           {
               System.Windows.Input.Cursor currentCur = App.MainView.Cursor;
               App.MainView.Cursor = System.Windows.Input.Cursors.Wait;
               try
               {
                   //dowloading the single image from internet
                   Task.Factory.StartNew(()=>{
                       WebRequest req = WebRequest.Create(result);
                       WebResponse response = req.GetResponse();
                       Stream stream = response.GetResponseStream();
                       Image im = Image.FromStream(stream);
                       App.CurrentEditingSong.ImageUrl = result;
                       App.CurrentEditingSong.Cover = im;
                   });
                   MessageBox.Show("Cover found", "Shoozla", MessageBoxButton.OK, MessageBoxImage.Information);

               }
               catch(Exception ie)
               {
                   System.Windows.MessageBox.Show("Cover not found! cannot download the cover, error: " + ie.Message, "Shoozla", MessageBoxButton.OK, MessageBoxImage.Exclamation);
               }
               finally{
                    App.MainView.Cursor = currentCur;
               }
           }
           else
               System.Windows.MessageBox.Show("Cover not found! Clean the artist and check the album name", "Shoozla",MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        public ID3TagEditorViewModel()
        {
            
        }
    }
}
